package com.kaisa.activity.fragment;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kaisa.activity.R;
import com.kaisa.activity.utils.FragmentTabUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by Administrator on 2015/12/12.
 * 个人中心
 */
@ContentView(R.layout.fragment_my)
public class MyFragmeng extends BaseFragment implements View.OnClickListener {
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    @ViewInject(R.id.landing_grp)
    private RadioGroup radioGroup;
    @ViewInject(R.id.lading_sina)
    private TextView sinaTextView;
    @ViewInject(R.id.lading_QQ)
    private TextView QQTextView;

    public static MyFragmeng newInstance() {
        MyFragmeng fragment = new MyFragmeng();
        return fragment;
    }

    @Override
    public void initView() {
        fragmentList.add(IDFragment.newInstance());
        fragmentList.add(NoIDFragment.newInstance());
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.landing_contanint, fragmentList.get(0)).commit();
        new FragmentTabUtils(getActivity().getSupportFragmentManager(), fragmentList, R.id.landing_contanint
                , radioGroup, null);
    }

    @Override
    public void initData() {
        sinaTextView.setOnClickListener(this);
        QQTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lading_sina:
                Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                threeLogin(platform);
                break;
            case R.id.lading_QQ:
                Platform platforms = ShareSDK.getPlatform(QQ.NAME);
                threeLogin(platforms);
                break;
        }
    }

    private void threeLogin(Platform platform) {
        if (platform == null) {
            return;
        }
        PlatformDb db = platform.getDb();
        if (db != null && db.isValid()) {
            String icon = db.getUserIcon();
            String username = db.getUserName();
            String id = db.getUserId();
        } else {
            //授权认证
            platform.setPlatformActionListener(new MyPlatformActionListener());
            platform.showUser(null);
        }
    }

    private class MyPlatformActionListener implements PlatformActionListener {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(Platform platform, int i) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK();
    }
}
